import DateUtil from "../util/date.js";
import RobotError from "../model/errors/robot-error.js";
import REDIS from "../util/redis.js";
import Player from "../model/player/player.js";


export default class Signin {

	async signin(player: Player) {
		if (await hasSignIn(player.qqNum)) throw new RobotError("今天已经签过到了");


		const COIN_REWARD = Math.floor(player.level * (Math.random() * 200 + 300));
		const EXP_REWARD = Math.floor(player.level * (Math.random() * 10 + 20));

		player.coin += COIN_REWARD;
		player.totalExp += EXP_REWARD;

		await recordSignin(player.qqNum);
		return `签到成功！获得了${COIN_REWARD}硬币和${EXP_REWARD}经验！`;
	}

}

async function recordSignin(qqNum: number) {
	await REDIS.set(`LNE:records:sign_in:${qqNum}`, new DateUtil().dateStr);
}

async function hasSignIn(qqNum: number) {
	let signInDate = await REDIS.get(`LNE:records:sign_in:${qqNum}`);
	return signInDate == new DateUtil().dateStr;
}
